﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EasyStep.TestExecution.Data;
using EasyStep.TestExecution;
using WPF.TestEditor.Properties;

namespace WPF.TestEditor.Controls.History
{
    /// <summary>
    /// Interaction logic for StepResult.xaml
    /// </summary>
    public partial class StepResult : UserControl
    {
        private TestStepResult _data;
        public StepResult(TestStepResult data)
        {
            _data = data;
            DataContext = new { 
                ResultIcon = GetImageIndex(_data.Result), 
                ResultText = _data.Browser, 
                ResultLog=_data.ExecutionLog, 
                ResultImg=string.IsNullOrEmpty(_data.ScreenshotFile)?null:new Uri(_data.ScreenshotFile) 
            };
            InitializeComponent();
            Loaded += new RoutedEventHandler(StepResult_Loaded);
        }

        void StepResult_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

       

        private string GetImageIndex(ExecutionResult result, bool forSelectedNode = false)
        {
            return GetNodeImage(result.ToString() + "16.png", forSelectedNode);
        }
        private string GetNodeImage(string key, bool selected = false)
        {
            return System.IO.Path.Combine(Settings.Default.ResourceFolder, key);
        }

        private void Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                try
                {
                    if (!string.IsNullOrEmpty(_data.ScreenshotFile) && System.IO.File.Exists(_data.ScreenshotFile))
                        using (var p = System.Diagnostics.Process.Start(_data.ScreenshotFile))
                        {
                            if (p != null) p.Close();
                        }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Can't open image!\r\n" + ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
            }
        }
    }
}
